package edu.hawaii.duedates.webApp;

import java.util.ArrayList;
import java.util.List;
import org.apache.wicket.Request;
import org.apache.wicket.protocol.http.WebSession;
import edu.hawaii.duedates.DueDates;
import edu.hawaii.duedates.ItemDue;
import edu.hawaii.duedates.configuration.DueDatesConfiguration;
import edu.hawaii.duedates.configuration.User;

/**
 * Creates a session for each user of the DueDates Application that gives each user
 * a personal Due Dates object.
 * 
 * @author Tyler Wolff
 */
public class DueDatesSession extends WebSession {

  /** Support serialization. */
  private static final long serialVersionUID = 1L;
  
  /** Each user has their own DueDates object. */
  private DueDates dueDates;
  private List<ItemDue> list = new ArrayList<ItemDue>();
  private List<ItemDue> uhList = new ArrayList<ItemDue>();
  private List<ItemDue> hslList = new ArrayList<ItemDue>();
  
  /** The configuration file for each session, created upon application start. */
  private DueDatesConfiguration config;
  
  /** The User of each session. */ 
  private User user = new User("defaultName", "defaultEmail", "defaultPassword");
  private String smtpServer;

  /** Holds user input for the login. */
  private String email = "";
  private String password = "";

  /** Various page elements throughout application. */
  private String alertResponse = "";
  private String errorMessage = "";
  private Integer resultsWithinUh;
  private Integer resultsWithinHsl;
  private Integer daysUntilDue = 7;
  private boolean alertsEnabled = false; 
  private boolean enableUhAlerts = false;
  private boolean enableHslAlerts = false;
  
  /**
   * Creates a new session for this user. Called automatically by Wicket.
   * 
   * @param request The request.
   */
  public DueDatesSession (Request request) {
    super(request);
  }
  
  /**
   * Return the DueDates instance for this specific user.
   * 
   * @return The DueDates object.
   */
  public DueDates getDueDatesObject() {
    return this.dueDates;
  }

  
  /**
   * Returns the logged-in user's data.
   * 
   * @return the logged-in user's data.
   */
  public User getUser() {
    return this.user;
  }
  
  /**
   * Sets the logged-in user's credentials data.
   * 
   * @param newUser User information.
   */
  public void setUser(User newUser) {
    this.user = newUser;
  }

 
  /**
   * Use to set the due dates object for this session.
   * 
   * @param args Arguments.
   */
  public void setDueDatesObject(String[] args) {
    this.dueDates = new DueDates(args);
  }
  
  /**
   * Returns the configuration settings.
   * 
   * @return The configuration settings.
   */
  public DueDatesConfiguration getDueDatesConfiguration() {
    return this.config;
  }

  /**
   * Sets the configuration settings.
   * 
   * @param config The configuration settings to set.
   */
  public void setDueDatesConfiguration(DueDatesConfiguration config) {
    this.config = config;
  }
  
  /**
   * Returns the email address of the user.
   * @return the email address of user.
   */
  public String getEmail() {
    return this.email;
  }

  /**
   * Sets the email address of the user.
   * 
   * @param newEmail The email address of user.
   */
  public void setEmail(String newEmail) {
    this.email = newEmail;
  }

  /**
   * Returns the within field.
   * @return The within field.
   */
  public Integer getWithinUh() {
    return this.resultsWithinUh;
  }

  /**
   * Sets the within field.
   * 
   * @param newWithin The within field value.
   */
  public void setWithinUh(Integer newWithin) {
    this.resultsWithinUh = newWithin;
    //this.dueDates.setWithin(newWithin == null ? 0 : Integer.valueOf(newWithin));
  }
  
  /**
   * Returns the within field for HSL.
   * @return The within field.
   */
  public Integer getWithinHsl() {
    return this.resultsWithinHsl;
  }

  /**
   * Sets the withinHsl field.
   * 
   * @param newWithin The within field value.
   */
  public void setWithinHsl(Integer newWithin) {
    this.resultsWithinHsl = newWithin;
    //this.dueDates.setWithin(newWithin == null ? 0 : Integer.valueOf(newWithin));
  }
  
  /**
   * Returns the password inputed by the user.
   * @return the password inputed by the user.
   */
  public String getPassword() {
    return this.password;
  }

  /**
   * Sets the password of the user.
   * 
   * @param newPassword The password of user.
   */
  public void setPassword(String newPassword) {
    this.password = newPassword;
  }

  /**
   * Sets the response message for a given session.
   * @param response the message to display.
   */
  public void setAlertResponse (String response) {
    this.alertResponse = response;
  }
  
  /**
   * Returns the current response message.
   * @return A response message.
   */
  public String getAlertResponse () {
    return this.alertResponse;
  }
  
  /**
   * Sets the error message for a given session.
   * @param error The error message to display.
   */
  public void setErrorMessage (String error) {
    this.errorMessage = error;
  }
  
  /**
   * Returns the current error message.
   * @return An error message.
   */
  public String getErrorMessage () {
    return this.errorMessage;
  }

  /**
   * Returns true if user specifies login data for the
   * Hawaii State Library.
   * @return true if user has HSL data.
   */
  public boolean hasHsl() {
    return (!("").equals(getUser().getHsl()));
  }
  
  /**
   * Returns true if user specifies login data for the
   * University of Hawaii Library.
   * @return true if user has UH data.
   */
  public boolean hasUh() {
    return (!("").equals(getUser().getUh()));
  }

  /**
   * Enable or disable alerts.
   * 
   * @param alertsEnabled Whether alerts are enabled.
   */
  public void setAlertsEnabled(boolean alertsEnabled) {
    this.alertsEnabled = alertsEnabled;
  }

  /**
   * The current value of the enabling alerts.
   * 
   * @return Whether alerts are enabled.
   */
  public boolean isAlertsEnabled() {
    return this.alertsEnabled;
  }

  /**
   * Sets the number of days ahead that alerts should look.
   * 
   * @param daysUntilDue The number of days alerts should look ahead.
   * 
   */
  public void setDaysUntilDue(String daysUntilDue) {
    this.daysUntilDue = Integer.valueOf(daysUntilDue);
  }

  /**
   * Returns the number of days ahead that alerts should look.
   * 
   * @return The number of days that alerts should look ahead.
   */
  public String getDaysUntilDue() {
    return this.daysUntilDue.toString();
  }

  /**
   * @param enableUhAlerts the enableUhAlerts to set
   */
  public void setEnableUhAlerts(boolean enableUhAlerts) {
    this.enableUhAlerts = enableUhAlerts;
  }

  /**
   * @return the enableUhAlerts
   */
  public boolean getEnableUhAlerts() {
    return this.enableUhAlerts;
  }

  /**
   * @param enableHslAlerts the enableHslAlerts to set
   */
  public void setEnableHslAlerts(boolean enableHslAlerts) {
    this.enableHslAlerts = enableHslAlerts;
  }

  /**
   * @return the enableHslAlerts
   */
  public boolean getEnableHslAlerts() {
    return this.enableHslAlerts;
  }

  /**
   * @param list the list to set
   */
  public void setList(List<ItemDue> list) {
    this.list = list;
  }

  /**
   * @return the list
   */
  public List<ItemDue> getList() {
    return this.list;
  }

  /**
   * @param uhList the uhList to set
   */
  public void setUhList(List<ItemDue> uhList) {
    this.uhList = uhList;
  }

  /**
   * @return the uhList
   */
  public List<ItemDue> getUhList() {
    return this.uhList;
  }

  /**
   * @param hslList the hslList to set
   */
  public void setHslList(List<ItemDue> hslList) {
    this.hslList = hslList;
  }

  /**
   * @return the hslList
   */
  public List<ItemDue> getHslList() {
    return this.hslList;
  }
  
  /**
   * Sets the smtp server name.
   * 
   * @param smtp The name of the server.
   */
  public void setSmtpServer(String smtp) {
    this.smtpServer = smtp;
  }
  
  /**
   * Gets the smtp server name.
   * @return the smtp server name.
   */
  public String getSmtpServer() {
    return this.smtpServer;
  }
}
